package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 老王
 * @since 2020-05-20
 */
@Api(tags = "章节管理相关接口")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @ApiOperation("添加章节")
    @PostMapping("/save")
    public R save(@RequestBody EduChapter chapter){
        boolean flag = chapterService.save(chapter);
        if (flag){
            return R.ok();
        }
        return R.error();
    }


    @ApiOperation("修改章节")
    @PostMapping("/update")
    public R update(@RequestBody EduChapter chapter){
        boolean flag = chapterService.updateById(chapter);
        if (flag){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/{chapterId}")
    public R delete(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("根据id获取章节")
    @GetMapping("/{chapterId}")
    public R getById(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }



}

